
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Xml;

using Swaf;
using Swaf.Crypto;
using Swaf.Resource;

namespace Swaf.Security
{
	/// <summary>
	/// Provides access to member properties for a security credential.  Values stored 
	/// in this class are retained in memory in an encrypted format for security purposes.
	/// </summary>

	public class SecurityCredential : ISecurityCredential
	{
		private String m_userId="";
		private String m_domain="";
		private String m_password="";
		private String m_catalogName="";
		private ICryptoManager m_cryptoManager;

		public SecurityCredential(XmlElement cred, string catalogName)
		{
			IResourceManager resMgr = Application.currentApp.resMgr;
			m_cryptoManager = (ICryptoManager) Application.currentApp.appData.get("sysObjs-Crypto");
			m_userId = m_cryptoManager.encrypt(resMgr.resolve(cred.GetAttribute("UserId")));
			m_domain = m_cryptoManager.encrypt(resMgr.resolve(cred.GetAttribute("Domain")));
			m_password = m_cryptoManager.encrypt(resMgr.resolve(cred.GetAttribute("Password")));
			m_catalogName = catalogName;
		}
		#region ISecurityCredential Members

		public string UserDomain
		{
			get
			{
				return m_cryptoManager.decrypt(m_domain);
			}
			set
			{
				m_domain = m_cryptoManager.encrypt(value);
			}
		}

		public string Password
		{
			get
			{
				return m_cryptoManager.decrypt(m_password);
			}
			set
			{
				m_password = m_cryptoManager.encrypt(value);
			}
		}

		public string UserId
		{
			get
			{
				return m_cryptoManager.decrypt(m_userId);
			}
			set
			{
				m_userId = m_cryptoManager.encrypt(value);
			}
		}

		#endregion
	}
}
